package com.zeedev.flyingsaucer.data;

import android.content.ContentValues;
import mobi.zeedev.flyingsaucer.common.DataModule.FlyingSaucerConv.Beers;
import mobi.zeedev.flyingsaucer.common.DataModule.FlyingSaucerConv.Members.MemberBeer;
import mobi.zeedev.flyingsaucer.common.DataModule.MetaBeer;
import mobi.zeedev.flyingsaucer.common.RegexModule.RegexUtil;

import java.util.*;

public class OldToNewConverter extends DatabaseConverter {

	

	public OldToNewConverter(List<ContentValues> inputList) {
		super(inputList);
	}

	//done
	protected static final Map<String,String> MetaBeerToMemberBeer = new HashMap<String,String>();
	protected static final Map<String,Destination> MetaBeerToDestinations = new HashMap<String,Destination>();
	protected static final Set<String> MetaBeerMemberBeerSet = new HashSet<String>();
	protected static final Set<String> MetaBeerSpecialCasesSet = new HashSet<String>();
	protected static final Set<String> MetaBeerBeerSet  =  new HashSet<String>();
	
	static {
//		MetaBeerToNewDatabase.put(key, value);
		String[][] MetaBeerToBeersMemberBeer = new String[][] {
				
				
				{ MetaBeer.COUNTRY_ID, Beers.COUNTRY_ID},
				{ MetaBeer.BREWER_ID, Beers.BREWER_ID},
				{ MetaBeer.STYLE_ID, Beers.STYLE_ID},
			
				{ MetaBeer.DESCRIPTION, Beers.DESCRIPTION},
				{ MetaBeer.CITY, Beers.CITY},
				{ MetaBeer.COUNTRY_NAME, Beers.COUNTRY_NAME},
				{ MetaBeer.STYLE_NAME, Beers.STYLE_NAME},
				{ MetaBeer.BREWER_NAME, Beers.BREWER_NAME},
				{ MetaBeer.AVERAGE_RATING, Beers.AVERAGE_RATING},
				
				{ MetaBeer.NAME, Beers.NAME},
				{ MetaBeer.CAN, Beers.CAN},
				{ MetaBeer.CONTAINER, Beers.CONTAINER},
				{ MetaBeer.DRAUGHT, Beers.DRAUGHT},
				{ MetaBeer.BOTTLE, Beers.BOTTLE},
				{ MetaBeer.NAME_SUBSCRIPT, Beers.NAME_SUBSCRIPT},
				
				//Special Cases
				{ MetaBeer.STATUS, MemberBeer.ACTIVE},
				{ MetaBeer.BEER_ID, Beers.BEER_ID},
				
				//Unknown
				//does not exist, no longer used
//				{ MetaBeer.RAW_NAME, MetaBeer.RAW_NAME},
				

				
				
				
				{ MetaBeer.INTERNAL_DATE_ADDED, MemberBeer.CREATION_DATE},
				{ MetaBeer.COMMENTS, MemberBeer.COMMENTS},
				{ MetaBeer.RATING, MemberBeer.RATING},
				{ MetaBeer.DATE_ADDED, MemberBeer.TASTED_DATE}
				
		};
		
		String[] BeerProperties = new String[] {
				MetaBeer.BEER_ID,
				MetaBeer.COUNTRY_ID,
				MetaBeer.BREWER_ID,
				MetaBeer.STYLE_ID,
				MetaBeer.DESCRIPTION,
				MetaBeer.CITY,
				MetaBeer.COUNTRY_NAME,
				MetaBeer.STYLE_NAME,
				MetaBeer.BREWER_NAME,
				MetaBeer.AVERAGE_RATING,
				MetaBeer.NAME,
				MetaBeer.CAN,
				MetaBeer.CONTAINER,
				MetaBeer.DRAUGHT,
				MetaBeer.BOTTLE,
				MetaBeer.NAME_SUBSCRIPT
				
		};
		
		String[] MetaBeerProperties = new String[] {
				MetaBeer.INTERNAL_DATE_ADDED,
				MetaBeer.COMMENTS,
				MetaBeer.RATING,
				MetaBeer.DATE_ADDED,
				MetaBeer.BEER_ID,
				MetaBeer.STATUS
		};
		
		String[] MetaBeerSpecialCases = new String[] {
				MetaBeer.STATUS,
				MetaBeer.BEER_ID
//				,
//				MetaBeer.COMMENTS,
//				MetaBeer.RATING,
//				MetaBeer.DATE_ADDED
		};
		
		for (String cur : MetaBeerProperties) {
			MetaBeerMemberBeerSet.add(cur);
		}
		
		for (String cur : BeerProperties) {
			MetaBeerBeerSet.add(cur);
		}
		
//		for (String[] pair : MetaBeerToBeersMemberBeer) {
//			if ( MetaBeerMemberBeerSet.contains(pair[0]) ) {
//				MetaBeerToDestinations.put(pair[0], Destination.MEMBERBEER);
//			}
//			if (MetaBeerBeerSet.contains(pair[0]) )  {
//				MetaBeerToDestinations.put(pair[0], Destination.BEERS);
//			}
//		}
		
		for (String[] pair : MetaBeerToBeersMemberBeer) {
			MetaBeerToMemberBeer.put(pair[0], pair[1]);
		}
		
		for (String cur : MetaBeerSpecialCases) {
			MetaBeerSpecialCasesSet.add(cur);
		}
		
	}

	@Override
	protected Destination[] chooseLists(String inputColumn) {

		List<Destination> destinationList = new ArrayList<Destination>(2);
//		
//		if ( !TextUtils.isEmpty( mapColumn(inputColumn, Destination.BEERS) )  ) {
//			destinationList.add(Destination.BEERS);
//			
//		}
//		if (	!TextUtils.isEmpty( mapColumn(inputColumn, Destination.MEMBERBEER) ) ) {
//			
//			destinationList.add(Destination.MEMBERBEER);
//		}
		
		if ( MetaBeerBeerSet.contains(inputColumn) ) {
			destinationList.add(Destination.BEERS);
			
		}
		
		if ( MetaBeerMemberBeerSet.contains(inputColumn) ) {
			destinationList.add(Destination.MEMBERBEER);
			
		}
		
		
		
//		if (destinatioList.size() < = 0 ) {
////			throw new IllegalStateException("There must be at least one output destination for this column.")
//			throw new IllegalArgumentException("You must specific a valid output destination for this column.");	
//		}
		
		return destinationList.toArray(new Destination[] {});
	}

	@Override
	protected String mapColumn(String inputColumn, Destination destination) {
		String ret = null;
//		if (destination == Destination.BEERS) {
//			ret =  MetaBeerToMemberBeer.get(inputColumn);
//		}
//		else if (destination == Destination.MEMBERBEER) {
			ret = MetaBeerToMemberBeer.get(inputColumn);
			if (ret == null) {
				throw new IllegalArgumentException("You must specify a valid input column.");	
			}
//		}
//		else {
//			throw new IllegalArgumentException("You must specify a valid destination column.");
//		}
		
		return ret;
	}

	@Override
	protected ContentValues mapValues(
			String inputKey, Object inputValueObject, Destination destination) {
		
		
		
	
		Object inputValue = inputValueObject;
		Map<String, Object> ret = new HashMap<String,Object>();
		if (inputValueObject == null) {
			return new ContentValues();
		}
		
		if ( MetaBeerSpecialCasesSet.contains(inputKey) ) { //inputKey == inputValue should also work..!//e(inputKey,inputValue)
			
			
			if ( e(inputKey,MetaBeer.STATUS) ) {
				if (destination == Destination.MEMBERBEER) {
					if ( e(inputValue, MetaBeer.STATUS_ACTIVE) ) {
						ret.put( MemberBeer.ACTIVE, true);
					}
					else if ( e(inputValue, MetaBeer.STATUS_INACTIVE) ) {
						ret.put( MemberBeer.ACTIVE, false);
					}
				}
			}
			
			else if ( e(inputKey,MetaBeer.BEER_ID) ) {
				if (destination == Destination.MEMBERBEER) {
					ret.put( MemberBeer.BEER_DB_ID, inputValue);	
				}
				else {
					ret.put( Beers.BEER_ID, inputValue);					
				}
			}
//			else if ( e(inputKey,MetaBeer.DATE_ADDED) ) {
//				if (destination == Destination.MEMBERBEER) {
//					if (!TextUtils.isEmpty(inputValue)) {
//						ret.put( MemberBeer.CREATION_DATE, inputValue);	
//					}
//						
//				}
//				else {				
//				}
//			}
			
			
		}
			
		else {
				ret.put(mapColumn(inputKey,destination) , inputValue);
		} 
			
		
//		switch (destination) {
//			case BEERS:
//				
//				
//				
//				break;
//			case MEMBERBEER:
//				
//				
//				
//				
//				break;
//			default:
//				throw new IllegalArgumentException("You must specify a valid destination column.");		
//		}
		
		
		ContentValues retCV;
		try {
			retCV = RegexUtil.induceContentValues(ret);
		} catch (Exception e) {
			e.printStackTrace();
			retCV = new ContentValues();
		}
			
		
		return retCV;
	}
	
	private static final Boolean e(Object left, Object right) {
		return (left + "").equals(right  + "");
	}

}
